import orderService from "@/serivce/orderService";
// import axios from "axios";
// import { getOrderList } from "@/api/order"


export default {
  namespaced: true,
  state: {
    statesList: [],
    orderList: [],
    skuList: [],
    orderDetailData: [],
    OrderApplyList: [],
    RefundSts: [],
 refundList: [],
    OrderDetails: [],
    RejectedData: [],
    OrderCauseList: []
  },

  getters: {
    getAllRefund(state) {
      return [...state.refundList]
    },
    getOrderList(state) {
      return [...state.orderList]
    },
    btngetCount(state) {
      return [...state.statesList]
    },
    //详情
    getorderDetailData(state) {
      return [...state.orderDetailData]
    },
    getSkuById(state) {
      return [...state.skuList]
    }
  },

  actions: {
    
      async getAllRefund({ commit }, { currentPage, pageSize, refundSts, orderId, userName, handelTime, sort }) {//获取退款所有列表
        try {
          let result = await orderService.getAllRefund(currentPage, pageSize, refundSts, orderId, userName, handelTime, sort);
          commit('setList', result.data.data)
          return result.data
        } catch (error) {
          console.log(error);
        }
      },

    async getOrderListFun({ commit }, { currentPage, pageSize }) {
      try {
        let result = await orderService.getOrderList(currentPage, pageSize);
        commit("getOrderList", result.data)
        return result.data
      } catch (err) {
        console.log(err);
      }
    },
    //状态数
    async btngetCountFun({ commit },) {
      try {
        let result = await orderService.btngetCount();
        commit("btngetCount", result.data)
        return result.data
      } catch (err) {
        console.log(err);
      }
    },
    //筛选
    async searchStoreInfoTwo({ commit }, [{ orderId, createTime }, callback]) {
      try {
        var { data } = await orderService.getSearchObjTwo(orderId, createTime);
        callback(data.data);
      } catch (err) {
        console.log(err);
      }
    },
    //筛选2
    async getstateFun({ commit }, data) {
      try {
        let result = await orderService.getstate(data);
        return result.data
      } catch (err) {
        console.log(err);
      }
    },

    //详情
    async getOrderDetailFun({ commit }, id) {
      try {
        let result = await orderService.getOrderDetailList(id);
        // console.log(result);
        commit("getOrderDetailList", result.data)
        return result.data
      } catch (err) {
        console.log(err);
      }
    },
    //库存
    async getSkuByIdFun({ commit }, id) {
      try {
        let result = await orderService.getSkuById(id);
        // console.log("resultsku", result);
        commit("getSkuById", result.data)
        return result.data
      } catch (err) {
        console.log(err);
      }
    },
    //修改
    async oderUpdateFun({ commit }, data) {
      try {
        let result = await orderService.OderUpdate(data);
        return result
      } catch (err) {
        console.log(err);
      }
    },
    //确认订单
    async confirmOrderFun({ commit }, id) {
      try {
        let result = await orderService.confirmOrder(id);
        return result
      } catch (err) {
        console.log(err);
      }
    },
    //关闭订单
    async closeOrderFun({ commit }, id) {
      try {
        let result = await orderService.closeOrder(id);
        return result
      } catch (err) {
        console.log(err);
      }
    },
    //批量确认订单
    async batchconfirmOrderFun({ commit }, data) {
      try {
        let result = await orderService.batchconfirmOrder(data);
        return result
      } catch (err) {
        console.log(err);
      }
    },
    //渲染页面数据
    async GetOrderApplyList({ commit }, { currentPage, pageSize }) {
      try {
        let result = await orderService.getOrderApplyList(currentPage, pageSize);
        // console.log(result);
        commit('setOrderApply', result.data)
        return result.data
      } catch (error) {
        console.log(error);
      }
    },


    //状态分类
    async StateClassification({ commit }, { refundSts }) {
      try {
        let result = await orderService.stateClassification(refundSts);
        // console.log('result',result)
        return result.data
      } catch (error) {
        console.log(error);
      }
    },


    //批量删除
    async delbatchRemoveById(context, ids) {
      try {
        let res = await orderService.delbatchRemoveById(ids);
        context.commit('setOrderApply', res.data)
        return res.data
      } catch (error) {
        console.log(error)
      }
    },


    //筛选查找
    async searchStoreInfo({ commit }, [{ orderId, userName }, callback]) {
      try {
        var { data } = await orderService.getSearchObj(orderId, userName);
        // console.log(data); 
        callback(data.data);
      } catch (error) {
        console.log(error);
      }
    },

    // 查看详情页
    async Requirements({ commit }, refundId) {
      try {
        console.log(refundId);
        let result = await orderService.getfindById(refundId);
        console.log(result.data);
        commit('setRetutnPending', result.data.data)
        return result.data
      } catch (error) {
        console.log(error);
      }
    },

    //同意退货
    async PutConfirmRefundBy({ commit }, { refundId, handlingOpinions, remarks }) {
      try {
        let result = await orderService.putConfirmRefundBy(refundId, handlingOpinions, remarks);
        return result.data
      } catch (error) {
        console.log(error);
      }
    },

    //拒绝退货
    async offRefund({ commit }, { refundId, handlingOpinions, remarks }) {
      try {
        let result = await orderService.putRefuseRefundBy(refundId, handlingOpinions, remarks);
        return result.data
      } catch (error) {
        console.log(error);
      }
    },

    //修改处理意见
    async ChangRemarks({ commit }, { refundId, remarks }) {
      try {
        let result = await orderService.putUpdateRefuse(refundId, remarks); 
        return result.data
      } catch (error) {
        console.log(error);
      }
    },
    async seachDetailsById({ commit }, { id }) {//通过id查看详情
      try {
        console.log('store', id);
        let result = await orderService.seachDetailsById(id)
        console.log("store", result.data.data);
        commit('setOrderDetails', result.data.data)
        return result.data.data
      } catch (err) {
        console.log(err);
      }
    },
    //同意退款
    async confirmRefundById({ commit }, { handlingOpinions, refundId, remarks }) {
      try {
        let result = await orderService.confirmRefundById(handlingOpinions, refundId, remarks);
        return result.msg
      } catch (err) {
        console.log(err);
      }
    },
    //拒绝退款
    async refuseRefundById({ commit }, { handlingOpinions, refundId, remarks }) {
      try {
        let result = await orderService.refuseRefundById(handlingOpinions, refundId, remarks);
        return result;
      } catch (err) {
        console.log(err);
      }
    },
    //修改处理意见
    async updateRefuseById({ commit }, { refundId, remarks }) {
      try {
        let result = await orderService.updateRefuseById(refundId, remarks)
        return result.data
      } catch (err) {
        console.log(err);
      }
    },
    //批量删除
    async delAllRejected({ commit }, ids) {
      try {
        let result = await orderService.batchRemove(ids)
        console.log("批量删除", result.data);
        return result.data
      } catch (err) {
        console.log(err);
      }
    },
    //获取退货原因的列表
    async getOrderReturns({ commit }, { currentPage, pageSize }) {
      try {
        let result = await orderService.getOrderReturns(currentPage, pageSize)
        commit("setOrderCause", result.data.data)
        console.log("退货原因的列表",result);
        return result.data
      } catch (error) {
        console.log(error);
      }
    },

    //请求数据条数
    async getExchangeCount({ commit }) {
      try {
        let result = await orderService.getExchangeCount();
        commit("getCount", result.data)
        return result.data

      } catch (error) {
        console.log(error);
      }
    },
     //退换原因批量删除
     async deleteByIds({commit},array){
      try{
        console.log(array);
        let result=await orderService.deleteByIds(array)
        return result.data
      }catch(error){
        console.log(error);
      }
    },
    //退换原因设置
    async addCauseList({commit},{orderreturnsId,cause,ordering,initiated}){
      try{
        let result=await orderService.addCauseList(orderreturnsId,cause,ordering,initiated)
        return result
      } catch (err){
        console.log(err);
      }
    },
    ////修改退款原因
    async updataBUId({commit},{orderreturnsId,cause,ordering,initiated}){
         try{
          let result=await orderService.updataBUId(orderreturnsId,cause,ordering,initiated)
          return result.data
         }catch(err){
          console.log(err);
         }
    },
    //退款统计
    async getcount({commit}){
      let result = await orderService.getRefundCount()
      return result.data
    }

  },

  mutations: {
    getOrderList(state, data) {
      //设置用户菜单
      state.orderList = data.data;
      // state.count = data.count
    },
    btngetCount(state, data) {
      state.statesList = data.data;
      console.log("btngetCount", data);
    },
    //详情
    getOrderDetailList(state, data) {
      //设置用户菜单
      console.log("详情", data);
      state.orderDetailData = data.data;
    },
    //库存
    getSkuById(state, data) {
      //设置用户菜单
      // console.log("库存", data);
      state.skuList = data.data;
    },
    //初始渲染
    setOrderApply(state, date) {
      state.OrderApplyList = date
    },
    //跳转页面渲染
    setRetutnPending(state, date) {
      state.ReturnList = date
    },

    getCount(state, data) {
      state.RefundSts = data.data;
    },
    setList(state, data) {
      state.refundList = data
    },
    setOrderDetails(state, data) {
      state.OrderDetails = data
    },
    setOrderCause(state, data) {
      state.OrderCauseList = data
    }

  },

}
